/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import java.util.*;
import javax.ejb.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import persistance.dao.SignumDAO;
import persistance.model.*;

/**
 *
 * @author Bobok
 */
@Singleton
public class JPASignumDAO implements SignumDAO {
    @PersistenceContext(unitName = "Rescriptus-ejbPU")
    private EntityManager em;

    public List<Signum> getAll() {
        System.out.println("Begin getAll()");
        List<Signum> markers = new ArrayList<Signum>();
        Query db = em.createQuery("select s from Signum s");
        List result = db.getResultList();
        System.out.println("Retrieved "+result.size()+" markers.");
        for(Object o : result){
            if (o instanceof Signum){
                markers.add((Signum)o);
            }else{
                throw new Error("Not a marker!"); //TODO: nie rzucać!
            }
        }
        System.out.println("End getAll()");
        return markers;
    }

    public Signum getDefault() {
        return getByName("Historical Event");
    }

    public Signum getByName(String name) {
        Query db = em.createQuery("select s from Signum s where s.name = :name");
        db.setParameter("name", name);
        List results = db.getResultList();
        return results.size() > 0 ? ((Signum)results.get(0)) : null;
    }
}
